package com.itheima.canal.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {
    //定义队列的名称
    public static final String AD_UPDATE_QUEUE = "ad_update_queue";
    //更新之前的消息队列,用于存放更新之前的位置信息
    public static final String AD_UPDATE_QUEUE_BEFORE = "ad_update_queue_before";

    //存放商品上架的消息队列
    public static final String SEARCH_UP_QUEUE = "search_up_queue";
    //定义商品上架时所连接的交换机的名称
    public static final String GOODS_UP_EXCHANGE = "goods_up_exchange";


    //定义商品下架的消息队列
    public static final String SEARCH_DOWN_QUEUE = "search_down_queue";
    //定义商品下架时所连接的交换机名称
    public static final String GOODS_DOWN_EXCHANGE = "goods_down_exchange";


    //提供创建队列的方法
    @Bean
    public Queue queue() {
        return new Queue(AD_UPDATE_QUEUE);
    }

    @Bean
    public Queue queue1() {
        return new Queue(AD_UPDATE_QUEUE_BEFORE);
    }

    @Bean(SEARCH_UP_QUEUE)
    public Queue GoodUpQueue() {
        return new Queue(SEARCH_UP_QUEUE);
    }

    //定义监听到商品上架时需要连接的交换机
    @Bean(GOODS_UP_EXCHANGE)
    public Exchange GoodsUpExchange() {
        return ExchangeBuilder.fanoutExchange(GOODS_UP_EXCHANGE).durable(true).build();
    }

    @Bean(SEARCH_DOWN_QUEUE)
    public Queue GoodDownQueue() {
        return new Queue(SEARCH_DOWN_QUEUE);
    }

    //定义监听到商品下架时需要连接的交换机
    @Bean(GOODS_DOWN_EXCHANGE)
    public Exchange GoodsDownExchange() {
        return ExchangeBuilder.fanoutExchange(GOODS_DOWN_EXCHANGE).durable(true).build();
    }


    //绑定队列和交换机的关系
    @Bean
    public Binding GoodUpQueueBinding(@Qualifier(SEARCH_UP_QUEUE) Queue queue, @Qualifier(GOODS_UP_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    //绑定下架队列和交换价的关系
    @Bean
    public Binding GoodDownQueueBinding(@Qualifier(SEARCH_DOWN_QUEUE) Queue queue, @Qualifier(GOODS_DOWN_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
}
